<template>
  <div>
    <div class="login" />

    <!--登录中间块-->
    <div class="login-mid">
      <div class="login-mid-top">
        <div class="shadow-top-left" />
        <div class="shadow-top-right" />
      </div>
      <div class="login-mid-mid">

        <!--捕获人脸区域-->
        <div class="videoCamera-canvasCamera">
          <video
            id="videoCamera"
            :width="videoWidth"
            :height="videoHeight"
            autoplay
          />
          <canvas
            id="canvasCamera"
            style="display: none"
            :width="videoWidth"
            :height="videoHeight"
          />

          <!--人脸特效区域-->
          <div v-if="faceImgState" class="face-special-effects-2" />
          <div v-else class="face-special-effects" />

        </div>

        <!--按钮区域-->
        <div class="face-btn">
          <button @click="faceVef()">{{ faceImgState?'正在识别中...':'开始识别' }}</button>
        </div>

        <!--消息区域-->
        <div class="msg">
          <div class="server-msg">{{ msg }}</div>
          <div class="welcome">Welcome to face recognition</div>
        </div>

      </div>
      <div class="login-mid-bot">
        <div class="shadow-bot-left" />
        <div class="shadow-bot-right" />
      </div>
    </div>

  </div>

</template>
<script>
import $camera from '../../camera/index.js'
import { login } from '../../api/user'
import { setToken } from '@/utils/auth'

export default {
  name: 'Face',
  data() {
    return {
      loginForm: {
      },
      videoWidth: 200,
      videoHeight: 200,
      msg: '',
      faceImgState: false,
      faceOption: {}
    }
  },
  mounted() {
    // 调用摄像头
    this.faceOption = $camera.getCamera({
      videoWidth: 200,
      videoHeight: 200,
      thisCancas: null,
      thisContext: null,
      thisVideo: null,
      canvasId: 'canvasCamera',
      videoId: 'videoCamera'
    })

    // this.getCompetence();
  },
  methods: {
    // 调用后台接口
    faceVef() {
      // 开始绘制图片
      const imageBase = $camera.draw(this.faceOption)
      if (this.faceImgState) {
        return
      }
      this.faceImgState = true
      if (imageBase === '' || imageBase === null || imageBase === undefined) {
        this.$message.error('图片数据为空')
      } else {
        console.log(this.faceOption)
        console.log(imageBase)
        this.loginForm.imagebase = imageBase
        login(this.loginForm).then(res => {
          // alert('你成功了')
          console.log(res)
          this.faceImgState = false
          // 关闭摄像头
          this.faceOption.thisVideo.srcObject.getTracks()[0].stop()
          this.$router.push({ path: this.redirect || '/dashboard' })
          window.location.reload()

          setToken(res.data.token)
        }, onerror => {
          this.faceImgState = false
          alert('您还未录入人脸')
        })
      }
    }
  }
}
</script>
<style>
@import "./index.css";
</style>
